package com.inventory.api.service;

import com.inventory.api.dao.ProductDao;
import com.inventory.api.dao.StockCheckTransactionDao;
import com.inventory.api.dao.StockTransactionDao;
import com.inventory.api.entity.Product;
import com.inventory.api.entity.StockCheckTransaction;
import com.inventory.api.entity.StockTransaction;
import com.inventory.api.signUpReq.StockCheckSignUpReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2018/2/2.
 */
@Service
public class StockCheckServiceImpl implements StockCheckService {

    @Autowired
    ProductDao productDao;

    @Autowired
    StockCheckTransactionDao stockCheckTransactionDao;


    @Autowired
    StockTransactionDao stockTransactionDao;


    @Override
    public void StockCheck(StockCheckSignUpReq signupReq) {
        /**
         * 所有库存变得记录
         */
        List<StockCheckTransaction> transactions = new ArrayList<>();
        /**
         * 商品出入库记录
         */
        List<StockTransaction> stockTransactions = new ArrayList<>();
        /**
         * 库存变得商品
         */
        List<Product> products = new ArrayList<>();
        /**
         * 所有店铺商品
         */
        Map<String, Product> productMap = productDao.findByStoreId(signupReq.getStoreId()).
                stream().collect(Collectors.toMap(Product::getId, account -> account));
        for (StockCheckSignUpReq.CheckProduct checkProduct : signupReq.getProducts()) {
            /**
             * 修改商品数量
             */
            Product product = productMap.get(checkProduct.getProductId());
            if (product == null) {
                product = new Product(checkProduct.getProductId(), signupReq.getStoreId(),
                        checkProduct.getProductName(), checkProduct.getCheckTotalQty());
            } else {
                product.setWarehouseQuantity(checkProduct.getCheckTotalQty());
            }
            products.add(product);

            /**
             * 如果数量改变则生成出入库记录
             */
            if (!checkProduct.checkSock()) {
                /**
                 * 生成出入库记录
                 */
                if (checkProduct.checkSockType()) {
                    StockTransaction stockTransaction = new StockTransaction(checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), checkProduct.getStockPrice(), signupReq.getId());
                    stockTransactions.add(stockTransaction);
                } else {
                    StockTransaction stockTransaction = new StockTransaction().StockIn(checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), checkProduct.getStockPrice(), signupReq.getId());
                    StockTransaction stockTransaction1 = new StockTransaction().StockOut(checkProduct, signupReq.getOperatorId(),
                            signupReq.getOperatorName(), signupReq.getStoreId(), checkProduct.getStockPrice(), signupReq.getId());
                    stockTransactions.add(stockTransaction);
                    stockTransactions.add(stockTransaction1);
                }
            }

            /**
             * 生成盘点记录
             */
            StockCheckTransaction transaction = new StockCheckTransaction(signupReq.getId(), signupReq.getStoreId(),
                    signupReq.getOperatorId(), signupReq.getOperatorName(), checkProduct);
            transactions.add(transaction);
        }
        productDao.save(products);
        stockCheckTransactionDao.save(transactions);
        stockTransactionDao.save(stockTransactions);

    }
}
